package com.taojin.shoppinginterest.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.activity.ListActivity;
import com.taojin.shoppinginterest.adapter.ListFragmentAdapter;
import com.taojin.shoppinginterest.bean.ListJson;
import com.taojin.shoppinginterest.bean.info;
import com.taojin.shoppinginterest.constants.ApiConfig;

import java.util.ArrayList;
import java.util.List;

public class ListFragment extends Fragment implements AdapterView.OnItemClickListener {
    @ViewInject(R.id.listFragment)
    private PullToRefreshListView pullToRefreshListView;
    private List<info> listAll;
    private ListFragmentAdapter listFragmentAdapter;
    private HttpUtils httpUtils;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_listfragment, null);
        ViewUtils.inject(this, view);
        initData();
        loadData();
        return view;
    }

    private void initData() {
        listAll = new ArrayList<info>();
        httpUtils = new HttpUtils();
        listFragmentAdapter = new ListFragmentAdapter(listAll, getActivity());
        pullToRefreshListView.setOnItemClickListener(this);
        pullToRefreshListView.setAdapter(listFragmentAdapter);
    }

    private RequestParams getRequestParams() {
        RequestParams requestParams = new RequestParams();
        requestParams.addBodyParameter("act", "top_list");
        requestParams.addBodyParameter("app", "other");
        requestParams.addBodyParameter("page", "1");
        requestParams.addBodyParameter("perpage", "20");
        requestParams.addBodyParameter("pt", "2");
        requestParams.addBodyParameter("ver", "1.1");
        return requestParams;
    }

    private void loadData() {
        httpUtils.send(HttpRequest.HttpMethod.POST, ApiConfig.URL, getRequestParams(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String data = responseInfo.result;
                ListJson listJson = JSON.parseObject(data, ListJson.class);
                if (listJson != null) {
                    if (listJson.getData() != null) {
                        if (listJson.getData().getList() != null && !listJson.getData().getList().isEmpty()) {
                            listAll.clear();
                            listAll.addAll(listJson.getData().getList());
                            listFragmentAdapter.notifyDataSetChanged();
                        }
                    }
                }
                pullToRefreshListView.onRefreshComplete();
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String ID=listAll.get(position-1).getId();
        Intent intent=new Intent(getActivity(), ListActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("ID",ID);
        intent.putExtras(bundle);

        startActivity(intent);

    }
}
